package com.exercise.animations.animation

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.transition.Fade
import android.view.LayoutInflater
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.exercise.animations.R
import com.exercise.animations.databinding.ActivityFadeInFadeOutBinding
import com.exercise.struct.base.BaseActivity
import com.exercise.struct.viewmodel.BaseViewModel

class FadeInFadeOutActivity : BaseActivity<BaseViewModel, ActivityFadeInFadeOutBinding>() {
    override val bindingInflater: (LayoutInflater) -> ActivityFadeInFadeOutBinding
        get() = ActivityFadeInFadeOutBinding::inflate
    override val modelClass: Class<BaseViewModel>
        get() = BaseViewModel::class.java

    private var shortAnimationDuration: Int = 0

    override fun initData() {
    }

    override fun initView() {
        binding.imageView.visibility = View.GONE
        shortAnimationDuration = resources.getInteger(android.R.integer.config_shortAnimTime)
        binding.progressBar.setOnClickListener {
            crossFade()
        }
    }

    private fun crossFade() {
        binding.imageView.apply {
            alpha = 0f
            visibility = View.VISIBLE
            animate()
                .alpha(1f)
                .setDuration(shortAnimationDuration.toLong())
                .setListener(null)
        }
        binding.progressBar
            .animate()
            .alpha(0f)
            .setDuration(shortAnimationDuration.toLong())
            .setListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    binding.progressBar.visibility = View.GONE
                }
            })
    }

    companion object {
        fun start(context: Context) {
            Intent(context, FadeInFadeOutActivity::class.java).also {
                context.startActivity(it)
            }
        }
    }

}